<template>
  <el-container class="home-container">
    <!-- 头部 -->
    <el-header>
      <div class="left">
        <img src="../assets/login01.png" alt />
        <span>电商后台管理系统</span>
      </div>
      <el-button type="info" size="small" @click="logout">退出登录</el-button>
    </el-header>
    <!-- 页面主体 -->
    <el-container>
      <!-- 侧边栏 -->
      <el-aside :width="isCollapse ? '64px' : '200px'">
        <!-- 折叠菜单 -->
        <div class="toggle-button" @click="toggleCollapse">
          <i :class="isCollapse ? 'el-icon-s-unfold' : 'el-icon-s-fold'"></i>
        </div>
        <!-- 侧边栏菜单 -->
        <el-menu
          :router="true"
          :collapse="isCollapse"
          :collapse-transition="false"
          :unique-opened="true"
          :default-active="activePath"
          background-color="#333744"
          text-color="#fff"
          active-text-color="#409BFF"
        >
          <el-submenu :index="item.id + ''" v-for="item in menuList" :key="item.id">
            <template slot="title">
              <i :class="iconsList[item.id]"></i>
              <span>{{ item.authName }}</span>
            </template>
            <el-menu-item
              :index="'/' + childItem.path"
              v-for="childItem in item.children"
              :key="childItem.id"
              @click="saveNavState('/' + childItem.path)"
            >
              <i class="el-icon-menu"></i>
              <span>{{ childItem.authName }}</span>
            </el-menu-item>
          </el-submenu>
        </el-menu>
      </el-aside>
      <!-- 右侧内容主体 -->
      <el-main :class="isCollapse ? 'left2' : 'left1'">
        <router-view></router-view>
      </el-main>
    </el-container>
  </el-container>
</template>

<script>
export default {
  data() {
    return {
      // 左侧菜单数据
      menuList: [],
      iconsList: {
        125: 'el-icon el-icon-user-solid',
        103: 'el-icon el-icon-s-cooperation',
        101: 'el-icon el-icon-s-goods',
        102: 'el-icon el-icon-s-order',
        145: 'el-icon el-icon-data-line'
      },
      // 左侧折叠
      isCollapse: false,
      // 被激活的连接地址
      activePath: ''
    }
  },
  created() {
    this.getMenuList()
    this.activePath = window.sessionStorage.getItem('activePath')
  },
  methods: {
    // 退出登录
    logout() {
      window.sessionStorage.clear()
      this.$message.success('退出成功！')
      this.$router.push('/login')
    },
    // 获取菜单
    async getMenuList() {
      const { data: res } = await this.$http.get('menus')
      if (res.meta.status !== 200) {
        return this.$message.console.error(res.meta.msg)
      }
      this.menuList = res.data
    },
    // 切换菜单展开与折叠
    toggleCollapse() {
      this.isCollapse = !this.isCollapse
    },
    // 保存连接激活状态
    saveNavState(activePath) {
      window.sessionStorage.setItem('activePath', activePath)
      this.activePath = activePath
    }
  }
}
</script>

<style lang="less" scoped>
.home-container {
  height: 100%;
  .el-header {
    position: relative;
    background-color: #373d41;
    display: flex;
    // 左右贴边
    justify-content: space-between;
    padding-left: 0;
    // 纵向居中
    align-items: center;
    color: #ffffff;
    font-size: 20px;
    .left {
      display: flex;
      align-items: center;
      img {
        height: 50px;
        width: 50px;
      }
      span {
        margin-left: 15px;
      }
    }
  }
  .el-aside {
    background-color: #333744;
    display: block;
    height: 100%;
    .toggle-button {
      background-color: #4a5064;
      font-size: 28px;
      line-height: 40px;
      color: #ffffff;
      text-align: center;
      letter-spacing: 0.2em;
      // 鼠标变小手
      cursor: pointer;
    }
    .el-menu {
      border-right: 0;
      .el-icon {
        margin-right: 10px;
      }
    }
  }
  .el-main {
    background-color: #eaedf1;
    position: absolute;
    right: 0;
    top: 60px;
    bottom: 0;
    overflow-y: auto;
  }
  .left1 {
    left: 200px;
  }
  .left2 {
    left: 64px;
  }
}
</style>
